package com.sskj.tibi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.RechargeBean;
import com.sskj.tibi.bean.WithdrawRechargeCoinBean;
import com.sskj.tibi.presenter.RechargeActivityPresenter;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;


@Route(path = RConfig.TIBI_RECHARGE)//充币
public class RechargeActivity extends BaseActivity<RechargeActivityPresenter> {
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    @BindView(R2.id.ivQR)
    ImageView ivQR;
    @BindView(R2.id.tvAddress)
    TextView tvAddress;
    @BindView(R2.id.btCopy)
    Button btCopy;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    private BottomSheetDialog bottomSheetCoin;

    @Override
    protected int getLayoutId() {
        return R.layout.tibi_activity_recharge;
    }

    @Override
    public RechargeActivityPresenter getPresenter() {
        return new RechargeActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.tibi_rechargeActivity1));
        setRightImg(R.mipmap.tibi_icon_record, v -> {
            ARouter.getInstance().build(RConfig.TIBI_RECHARGE_RECORD).navigation();
        });
    }

    @Override
    protected void initData() {
        mPresenter.coinList();
    }

    public void updateUI(RechargeBean data) {
        ImageUtil.setImage(data.getQrcode(), ivQR);
        ivQR.setOnLongClickListener(v -> {
            ImgUtil.saveImageToGallery(this, ivQR);
            return true;
        });
        tvAddress.setText(data.getAddress());
        ClickUtil.click(btCopy, () -> {
            CopyUtil.copy(data.getAddress());
        });

    }

    public void updateCoinList(List<WithdrawRechargeCoinBean> data) {
        heySpinner.setText(data.get(0).getCode());
        btCopy.setText(String.format(App.INSTANCE.getString(R.string.tibi_rechargeActivity2), data.get(0).getCode()));
        mPresenter.getData(data.get(0).getCode());
        Flowable.fromIterable(data)
                .map(withdrawRechargeCoinBean -> withdrawRechargeCoinBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                            ClickUtil.click(heySpinner, () -> {
                                bottomSheetCoin = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                                    bottomSheetCoin.dismiss();
                                    heySpinner.setText(data.get(i).getCode());
                                    btCopy.setText(String.format(App.INSTANCE.getString(R.string.tibi_rechargeActivity2), data.get(i).getCode()));
                                    mPresenter.getData(data.get(i).getCode());
                                }, strings);
                                bottomSheetCoin.show();
                            });

                        }
                );
    }
}
